package com.szrt.easyui.buybuyup.user.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.szrt.easyui.buybuyup.common.MessageDTO;
import com.szrt.easyui.buybuyup.company.dto.CompanyDTO;
import com.szrt.easyui.buybuyup.user.dao.UserDao;
import com.szrt.easyui.buybuyup.user.entity.UserEntity;

@Service
public class UserService {
	
	@Autowired
	private UserDao dao;

	@Transactional(readOnly=true)
	public void findAllPage(Map<String, Object> fenye, Map<String, Object> reuslt) {
		try{
			List<UserEntity> rows = dao.findAllPage(fenye);
			
			int total =dao.counterEntity(fenye);
			
			//EasyUI 规定最少要2个数据给到前台  
			//1:告诉前台一共有多少条数据total
			//2:我这一页有多少条记录 rows
			reuslt.put("rows", rows);
			reuslt.put("total", total);
			MessageDTO.putSuccessMessage(reuslt);
		}catch(Exception e){
			MessageDTO.putErrorMessage(reuslt);
			e.printStackTrace();
		}
		
	}
	@Transactional(readOnly=true)
	public void findAllPageV2(Map<String, Object> fenye, Map<String, Object> reuslt) {
		try{
			List<UserEntity> rows = dao.findAllPageV2(fenye);
			
			int total =dao.counterEntity(fenye);
			
			//EasyUI 规定最少要2个数据给到前台  
			//1:告诉前台一共有多少条数据total
			//2:我这一页有多少条记录 rows
			reuslt.put("rows", rows);
			reuslt.put("total", total);
			MessageDTO.putSuccessMessage(reuslt);
		}catch(Exception e){
			MessageDTO.putErrorMessage(reuslt);
			e.printStackTrace();
		}
		
	}

	@Transactional(propagation =Propagation.REQUIRED,rollbackFor=RuntimeException.class)
	public void add(UserEntity entity, Map<String, Object> reuslt) throws RuntimeException {
		try{
			
			//影响的记录的行数
			dao.add(entity);
			
			//EasyUI 规定最少要2个数据给到前台  
			//1:告诉前台一共有多少条数据total
			//2:我这一页有多少条记录 rows
			MessageDTO.putSuccessMessage(reuslt);
		}catch(Exception e){
			MessageDTO.putErrorMessage(reuslt);
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}

	
	@Transactional(propagation =Propagation.REQUIRED,rollbackFor=RuntimeException.class)
	public void update(UserEntity entity, Map<String, Object> reuslt) throws RuntimeException {
		try{
			
			//影响的记录的行数
			dao.update(entity);
			
			//EasyUI 规定最少要2个数据给到前台  
			//1:告诉前台一共有多少条数据total
			//2:我这一页有多少条记录 rows
			MessageDTO.putSuccessMessage(reuslt);
		}catch(Exception e){
			MessageDTO.putErrorMessage(reuslt);
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}

	@Transactional(readOnly=true)
	public UserEntity findById(Integer id) {
		UserEntity entity  = null;
		try{
				
			//影响的记录的行数
			entity =dao.findById(id);
			
			//EasyUI 规定最少要2个数据给到前台  
			//1:告诉前台一共有多少条数据total
			//2:我这一页有多少条记录 rows
		}catch(Exception e){
			e.printStackTrace();
		}
		return entity;
		
	}

	
	@Transactional(propagation =Propagation.REQUIRED,rollbackFor=RuntimeException.class)
	public void deleteByIds(List<Integer> ids, Map<String, Object> reuslt) throws RuntimeException {
		try{
					
				//影响的记录的行数
				dao.deleteByIds(ids);
				
				//EasyUI 规定最少要2个数据给到前台  
				//1:告诉前台一共有多少条数据total
				//2:我这一页有多少条记录 rows
				MessageDTO.putSuccessMessage(reuslt);
			}catch(Exception e){
				MessageDTO.putErrorMessage(reuslt);
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
	public List<CompanyDTO> findAllKeyAndValue() {
		return dao.findAllKeyAndValue();
	}
}
